<template>
  <div v-loading="loading" class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">姓名：</label>
        <el-input v-model="query.name" clearable placeholder="请输入姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">预约电话：</label>
        <el-input v-model="query.bookingPhone" clearable placeholder="请输入预约电话" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">预约时间：</label>
        <el-date-picker
          v-model="query.startTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
        <label class="el-form-item-label">至</label>
        <el-date-picker
          v-model="query.endTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间">
        </el-date-picker>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="bookingPhone" label="预约电话" />
        <el-table-column prop="type" label="餐饮类型" >
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span v-if="scope.row.type == 1">早餐</span>
            <span v-else-if="scope.row.type == 2">午餐</span>
            <span v-else-if="scope.row.type == 3">晚餐</span>
            <span v-else-if="scope.row.type == 4">其他</span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column prop="number" label="预约人数" />
        <el-table-column prop="bookingTime" label="预约时间" />
        <el-table-column prop="netPrice" label="实际价格" >
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span style="color: red">{{scope.row.netPrice}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="refundStatus" label="退款状态" width="150">
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span v-if="scope.row.refundStatus === 1">退款中</span>
            <span v-else-if="scope.row.refundStatus === 2">线上退款</span>
            <span v-else-if="scope.row.refundStatus === 3">线下退款</span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column prop="refundPrice" label="退款金额" width="150" >
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span style="color: green">{{scope.row.refundPrice}}</span>
          </template>
        </el-table-column>
<!--        <el-table-column prop="status" label="状态">-->
<!--          <template slot-scope="scope">-->
<!--            &lt;!&ndash; 根据不同的值显示不同的内容 &ndash;&gt;-->
<!--            <span v-if="scope.row.status === 1">待接单</span>-->
<!--            <span v-else-if="scope.row.status === 2">已接单</span>-->
<!--            <span v-else-if="scope.row.status === 3">已完成</span>-->
<!--            <span v-else-if="scope.row.status === 4">拒绝</span>-->
<!--            <span v-else-if="scope.row.status === 5">退回</span>-->
<!--            <span v-else-if="scope.row.status === 6">撤回</span>-->
<!--            <span v-else />-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column prop="createTime" label="创建日期" />
        <el-table-column label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-button v-if="scope.row.refundStatus == 1" size="small" type="primary" @click="refundDialogVisibleClick(scope.row.id)">退款</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />

      <!-- 输入框对话框 -->
      <el-dialog
        title="退款"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form ref="form" label-width="80px" >
          <el-form-item label="退款金额">
            <el-input-number v-model="orderAmount" :precision="2" :step="0.1" :min="0" :max="999"></el-input-number>
          </el-form-item>
          <el-form-item label="退款方式">
            <el-radio v-model="refundType" label="2">线上退款</el-radio>
            <el-radio v-model="refundType" label="3">线下退款</el-radio>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleClick">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudCanteenOrders from '@/api/system/canteenOrders'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import refund from '@/api/system/refund'

const defaultForm = { id: null, createTime: null, updateTime: null, createBy: null, updateBy: null, deleted: null, xqId: null, type: null, canteenId: null, name: null, number: null, bookingTime: null, bookingPhone: null, status: null, netPrice: null, canteenDetailId: null }
export default {
  name: 'CanteenOrdersRefund',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '食堂预约退款', url: 'api/canteenOrders', query: { refundStatus: [1, 2, 3] }, idField: 'id', sort: 'id,desc', crudMethod: { ...crudCanteenOrders }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'canteenOrders:add'],
        edit: ['admin', 'canteenOrders:edit'],
        del: ['admin', 'canteenOrders:del']
      },
      rules: {
      },
      dialogVisible: false, // 控制对话框显示隐藏
      // inputValue: '', // 输入框的值
      loading: false,
      dialogAmountVisible: false,
      currentId: 0,
      orderAmount: 0,
      refundType: null
    }
  },
  methods: {
    // 关闭对话框前可进行的操作
    handleClose() {
      // 这里可以添加关闭前需要执行的逻辑
      this.orderAmount = 0
      this.dialogVisible = false
    },
    // 退款
    handleClick() {
      if (!this.orderAmount) {
        this.$message({
          message: '请选择退款金额',
          type: 'warning'
        })
        return false
      }
      const data = { 'orderId': this.currentId, 'amount': this.orderAmount, 'type': 3, 'refundPrice': (this.orderAmount).toString() }
      this.loading = true
      if (this.refundType === '2') { // 线上退款
        refund.refund(data).then(data => {
          this.orderAmount = 0
          this.currentId = null
          this.refundType = null
          this.loading = false
          this.dialogVisible = false
          this.crud.refresh()
        }).catch(() => {
          this.loading = false
        })
      } else if (this.refundType === '3') {
        refund.offlineRefund(data).then(data => {
          this.orderAmount = 0
          this.currentId = null
          this.refundType = null
          this.loading = false
          this.dialogVisible = false
          this.crud.refresh()
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.$message({
          message: '请选择退款类型',
          type: 'warning'
        })
        return false
      }
    },
    refundDialogVisibleClick(id) {
      this.currentId = id
      this.dialogVisible = true
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.optShow.add = false
      this.crud.optShow.edit = false
      this.crud.optShow.del = false
      this.crud.optShow.download = false
      return true
    }
  }
}
</script>

<style scoped>

</style>
